package com.bluegalaxy.sitescore.service.impl;

import com.bluegalaxy.sitescore.entity.Air;
import com.bluegalaxy.sitescore.entity.TbAirScore;
import com.bluegalaxy.sitescore.mapper.TbAirScoreMapper;
import com.bluegalaxy.sitescore.service.ITbAirScoreService;
import com.bluegalaxy.sitescore.service.support.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

@Service
public class TbAirScoreServiceImpl extends BaseServiceImpl<TbAirScoreMapper,TbAirScore> implements ITbAirScoreService {

    @Autowired
    private TbAirScoreMapper tbAirScoreMapper;

    @Override
    public boolean insertAirScore(Air air,int sourceId) {
        TbAirScore tbAirScore = caculateAirScore(air,sourceId);
        if(tbAirScore != null){
            if(tbAirScoreMapper.insert(tbAirScore)==1){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }


    }

    @Override
    public boolean updateAirScore(Air air, int sourceId) {
        TbAirScore tbAirScore = caculateAirScore(air,sourceId);
        int id = getAirBySid(sourceId).getId();
        if(tbAirScore != null){
            tbAirScore.setId(id);
            if(tbAirScoreMapper.updateById(tbAirScore)==1){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public TbAirScore caculateAirScore(Air air, int sourceId) {
        TbAirScore tbAirScore = new TbAirScore();
        int ObsRelease =0;
        int gasPotentialToRelease=0;
        int particulatePotenToRelease=0;
        int nearestIntake=0;
        int popConcent1=0;
        int popConcent2=0;
        int potConcent=0;
        int resources=0;
        int actualContamination=0;
        int potentialContamination=0;
        Date date = new Date();

        if(air.getObsRelease()!=550&&air.getObsRelease()!=0){
            return null;

        }else {
            ObsRelease = air.getObsRelease();
        }
        if(air.getGasPotentialToRelease()<0){
            return null;

        }else {
            gasPotentialToRelease = air.getGasPotentialToRelease();
        }
        if (air.getParticulatePotenToRelease()<0){
            return null;

        }else {
            particulatePotenToRelease = air.getParticulatePotenToRelease();
        }
        //0,1,2,7,20,45,50
        if(air.getNearestIntake()!=0&&air.getNearestIntake()!=1&&air.getNearestIntake()!=2&&air.getNearestIntake()!=7&&air.getNearestIntake()!=20&&air.getNearestIntake()!=45&&air.getNearestIntake()!=50){
            return null;

        }else {
            nearestIntake = air.getNearestIntake();
        }
        if(air.getPopConcent1()<0){
            return null;

        }else {
            popConcent1 = air.getPopConcent1();
        }
        if(air.getPopConcent2()<0){
            return null;

        }else {
            popConcent2 = air.getPopConcent2();
        }
        if(air.getPotConcent()<0){
            return null;

        }else {
            potConcent = air.getPotConcent();
        }
        if(air.getResources()!=0&&air.getResources()!=5){
            return null;
        }else {
            resources = air.getResources();
        }
        if(air.getActualContamination()<0){
            return null;

        }else {
            actualContamination = air.getActualContamination();
        }
        if(air.getPotentialContamination()<0){
            return null;

        }else {
            potentialContamination =air.getPotentialContamination();
        }

        int lr = 0;
        BigDecimal nearbyT = null;
        BigDecimal Uscore = null;
        BigDecimal score = null;
        //未计算
        int wc = air.getWasteCharacteristics();

        //计算LR
        if(ObsRelease == 550){
            lr = 550;
        }else {
            lr = gasPotentialToRelease>particulatePotenToRelease?gasPotentialToRelease:particulatePotenToRelease;
        }
        //   System.out.println("lr:"+lr);
        BigDecimal pop =
                new BigDecimal(popConcent1).multiply(new BigDecimal(10)).add(
                        new BigDecimal(popConcent2)).add(
                        new BigDecimal(potConcent).multiply(
                                new BigDecimal(0.1)));
        //  System.out.println("pop:"+pop);
        //计算T
        nearbyT = new BigDecimal(nearestIntake).add(pop).add(
                new BigDecimal(resources)).add(
                new BigDecimal(actualContamination)).add(
                new BigDecimal(potentialContamination).multiply(new BigDecimal(0.1)));
        //  System.out.println("nearbyT:"+nearbyT);

        //计算score
        Uscore = new BigDecimal(lr).multiply(new BigDecimal(wc)).multiply(nearbyT).divide(new BigDecimal(82500),2, RoundingMode.HALF_UP);
        if (Uscore.floatValue()>100){
            score = BigDecimal.valueOf(100);
        }else {
            score = Uscore;
        }

        //保存操作
        tbAirScore.setSource_id(sourceId);
        tbAirScore.setObs_release(air.getObsRelease());
        tbAirScore.setGas_potential_to_release(air.getGasPotentialToRelease());
        tbAirScore.setParticulate_poten_to_release(air.getParticulatePotenToRelease());
        tbAirScore.setPotential_to_release(air.getPotentialToRelease());
        tbAirScore.setLikelihood_of_release(lr);
        //未知
        tbAirScore.setMobility_type(1);
        tbAirScore.setMobility_value(1);
        tbAirScore.setHas_waste_quantity(air.getHasWasteQuantity());
        tbAirScore.setWaste_characteristics(air.getWasteCharacteristics());
        tbAirScore.setNearest_intake(air.getNearestIntake());
        tbAirScore.setPop_concent1(air.getPopConcent1());
        tbAirScore.setPop_concent2(air.getPopConcent2());
        tbAirScore.setPot_concent(air.getPotConcent());
        //此处应为float
        tbAirScore.setPopulation(pop.floatValue());
        tbAirScore.setResources(air.getResources());
        tbAirScore.setActual_contamination(air.getActualContamination());
        tbAirScore.setPotential_contamination(air.getPotentialContamination());
        tbAirScore.setSensitive_environments(air.getSensitiveEnvironments());
        tbAirScore.setTargets(nearbyT.floatValue());
        tbAirScore.setUncapped_score(Uscore.floatValue());
        tbAirScore.setAir_pathway_score(score.floatValue());
        tbAirScore.setUpdate_time(date);
        tbAirScore.setCreate_time(date);
        tbAirScore.setIs_delete(0);
        return tbAirScore;
    }

    @Override
    public TbAirScore getAirBySid(int sid) {
        TbAirScore tbAirScore = new TbAirScore();
        tbAirScore.setSource_id(sid);
        return tbAirScoreMapper.selectOne(tbAirScore);
    }

    @Override
    public boolean isAirExist(int sid) {
        TbAirScore airScore = new TbAirScore();
        airScore.setSource_id(sid);
        int count = tbAirScoreMapper.selectCount(airScore);
        if(count==0){
            return false;
        }
        return true;
    }
}
